package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品相关接口
 */
@Api(tags = "菜品相关接口")
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    /**
     * 导入业务层接口
     */
    @Autowired
    private DishService dishService ;
    @Autowired
    private RedisTemplate redisTemplate ;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}" , dishDTO);
        dishService.saveWithFlavors(dishDTO) ;

        //精确清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId() ;
        ClearCache(key);
        return Result.success() ;
    }


    /**
     * 菜品分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO)  {
        log.info("菜品分页查询:{}",dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO) ;
        return Result.success(pageResult) ;
    }

    /**
     * 批量删除菜品数据
     * @RequestParam 注解用于接收集合参数
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品数据")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除菜品数据；{}" , ids);
        dishService.deleteBatch(ids) ;
        //很难精确清理缓存数据 所以清理所有缓存数据
        ClearCache("dish_*");
        return Result.success() ;
    }

    /**
     * 修改菜品数据
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品数据")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品数据：{}" , dishDTO);
        dishService.updateWithFlavors(dishDTO) ;
        //很难精确清理缓存数据 所以清理所有缓存数据
        ClearCache("dish_*");
        return Result.success() ;
    }

    /**
     * 根据id查询菜品数据
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品数据")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id ) {
        log.info("根据id查询菜品数据：{}" , id);
        DishVO dishVO = dishService.getById(id) ;
        return Result.success(dishVO) ;
    }

    /**
     * 菜品的起售和停售
     * @return
     */
    @ApiOperation("菜品的起售和停售")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info(status == 1 ? "修改菜品状态为起售" : "修改菜品状态为停售" , id);
        dishService.startOrStop(status,id) ;
        ClearCache("dish_*");
        return Result.success() ;
    }

    /**
     * 根据分类id查询菜品
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> getByCategoryId(Long categoryId) {
        log.info("根据分类 id 查询菜品：{}" , categoryId);
        List<DishVO> dishList = dishService.getByCategoryId(categoryId) ;
        return Result.success(dishList) ;
    }

    /**
     * 清理缓存
     */
    public void ClearCache(String pattern) {
        log.info("数据发生变化 清理缓存...");
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys) ;
    }
}
